package com.xhsj.user.lsm.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhsj.user.hr.system.mapper.TbGlobalUserMapper;
import com.xhsj.user.lsm.entity.TbBadgesManager;
import com.xhsj.user.lsm.mapper.TbBadgesManagerMapper;
import com.xhsj.user.sms.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 * 证章管理 服务实现类
 * </p>
 *
 * @author suxiaolin
 * @since 2020-03-16
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
//@CacheConfig(cacheNames = "TbBadgesManager")
public class TbBadgesManagerService extends BaseService<TbBadgesManagerMapper, TbBadgesManager> {
    @Autowired
    private TbBadgesManagerMapper dao;

    @Autowired
    private TbGlobalUserMapper tbGlobalUserMapper;

    @Override
    //@CacheEvict(cacheNames="TbBadgesManager", allEntries=true)
    public boolean save(TbBadgesManager entity) {
        return super.save(entity);
    }

    @Override
    //@Cacheable(key = "#id",sync = true)
    public TbBadgesManager getById(Serializable id) {
        return super.getById(id);
    }

    @Override
    //@CacheEvict(cacheNames="TbBadgesManager", allEntries=true)
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    //@CacheEvict(cacheNames="SysDict", allEntries=true)
    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        return super.removeByIds(idList);
    }

    //@Cacheable(key = "#data",sync = true)
    public IPage<TbBadgesManager> selectPage(Map<String,Object> data) {
        int currentPage=1;
        if (!Objects.isNull(data.get("page"))) {
            currentPage=(Integer) data.get("page");
        }
        int pageSize=20;
        if (!Objects.isNull(data.get("size"))) {
            pageSize=(Integer) data.get("size");
        }

        TbBadgesManager entity = JSON.parseObject(JSON.toJSONString(data), TbBadgesManager.class);
        Page<TbBadgesManager> page = new Page(currentPage,pageSize);
        QueryWrapper<TbBadgesManager> wrapper = new QueryWrapper(entity);
        return super.selectPage(page, wrapper);
    }

    @Override
    //@Cacheable(value = "TbBadgesManager",sync = true)
    public List<TbBadgesManager> selectAll() {
        return super.selectAll();
    }

    //获取所有的部门信息
    public List<Map<String,Object>> getAllDept() {
        return tbGlobalUserMapper.getAllDept();
    }

//    根据类别获取证照/章名称
    public List<Map<String,Object>> getBadgesNameListByType(String type) {
//        QueryWrapper wrapper = new QueryWrapper();
//        wrapper.select("id AS value","badges_name AS label");
//        wrapper.eq("type",type);
//        List<Map<String,Object>> list = dao.selectMaps(wrapper);
        return dao.getBadgesNameListByType(type);
    }

    //修改证章管理使用状态 0 空闲 1 使用
    public boolean updateBadgesManagerUseStatus(int useStatus,String badgesManagerId) throws Exception {
        List<TbBadgesManager> tbBadgesManagerList = new ArrayList();
        String[] split = badgesManagerId.split(",");
        for (String id:split
             ) {
            TbBadgesManager tbBadgesManager  = new TbBadgesManager();
            tbBadgesManager.setUseStatus(useStatus);
            tbBadgesManager.setId(id);
            tbBadgesManagerList.add(tbBadgesManager);
        }

        return  this.updateBatchById(tbBadgesManagerList);
    }

    //获取证章管理列表

    public List<Map<String,Object>> badgesManagerList(int type) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("type",type);
        queryWrapper.select("badges_name AS label","id AS value");
        return  dao.selectMaps(queryWrapper);
    }
}
